#include "pch.hpp"
#include "InfoReader.hpp"

#include <engine.core/qt_stl_integration.hpp>




DatabaseInfoReader::DatabaseInfoReader(const QString& fname, const ItemFactory& itemFactory, const LoggerPtr& logger, const DatabaseSystemPtr& database)
	: m_logger(logger)
	, m_itemFactory(itemFactory)
	, m_database(database)
	, m_file(qP(fname))
	, m_reader(m_file)
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




LOOSE_PTR(DatabaseItem) DatabaseInfoReader::read()
{
	Properties properties;

	try
	{
		m_reader.read_next(properties);
	}
	catch(const info_parser_error&)
	{
		return LOOSE_PTR(DatabaseItem)();
	}

	// We only need to know the type we're creating
	QString type = properties.get<std::string>("type").c_str();

	// The factory takes care of creating that type
	return m_itemFactory.create(type, properties);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




DatabaseReaderPtr DatabaseInfoReader::create(const QString& fname, const ItemFactory& itemFactory, const LoggerPtr& logger, const DatabaseSystemPtr& database)
{
	return DatabaseReaderPtr(new DatabaseInfoReader(fname, itemFactory, logger, database));
}
///////////////////////////////////////////////////////////////////////////////////////////////////
